﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.DomainServices;
using System.Web.Ria;
using SoftSource.Timecard.Web.DAL;
using SoftSource.Timecard.Web.Library;

namespace SoftSource.Timecard.Web.Services
{
    [EnableClientAccess]
    [RequiresAuthentication]
    [RequiresRole("Administrator", "Project Manager")]
    public class UtilityService : DomainService
    {
        public DateTime GetProjectCreationDate(int projectID)
        {
            return Projects.GetProjectCreationDate(projectID);
        }

        public void SendReminders(
            string password,
            IEnumerable<int> userIDs,
            string messageBody)
        {
            this.SendEmail(
                password,
                "Update Timecard",
                messageBody,
                userIDs,
                null,
                true);
        }

        public void SendEmail(
            string password,
            string subject,
            string messageBody,
            IEnumerable<int> toIDs,
            IEnumerable<int> ccIDs,
            bool seperateToAddresses)
        {
            string fromAddress;
            List<string> toAddresses = new List<string>();
            List<string> ccAddresses = new List<string>();

            object currentID = HttpContext.Current.Profile.GetPropertyValue("UserID");
            if (currentID != null && currentID is int)
            {
                int currentUserID = (int)currentID;

                fromAddress = Users.GetUserEmail(currentUserID);

                if (toIDs != null)
                {
                    foreach (int id in toIDs)
                    {
                        toAddresses.Add(Users.GetUserEmail(id));
                    }
                }

                if (ccIDs != null)
                {
                    foreach (int id in ccIDs)
                    {
                        ccAddresses.Add(Users.GetUserEmail(id));
                    }
                }

                SendMessage.SendEmail(
                    password,
                    subject,
                    messageBody,
                    fromAddress,
                    toAddresses,
                    ccAddresses,
                    seperateToAddresses);
            }
        }
    }
}


